import { defineStore } from "pinia";
import { ref, watch } from "vue";
import type { PlayerStore } from "@/store/usePlayer";
import type { LiveDataStore } from "./LiveDataStore";
import { answer } from "@/views/LiveNew/LiveNewApi";
import { CourseWatchEntity } from "@/typings/course";
export const useQuestionStore = defineStore("question", () => {
  const isVisible = ref(false);
  const isAnswer = ref(0);
  const isComplete = ref(false);
  const isFail = ref(false);

  const setIsVisible = (value: boolean) => {
    isVisible.value = value;
  };

  const setIsAnswer = (value: number) => {
    isAnswer.value = value;
  };

  const setIsComplete = (value: boolean) => {
    isComplete.value = value;
  };

  const setIsFail = (value: boolean) => {
    isFail.value = value;
  };

  const handleAnswer = async (
    liveDataStore: LiveDataStore,
    isRight: boolean
  ) => {
    if (isRight) {
      setIsComplete(true);
    } else {
      setIsFail(true);
    }
    liveDataStore.roomWatchData.isAnswer = 1
    liveDataStore.roomWatchData.answerState = isRight ? 1 : 2
    const roomWatchData = await answer({ ...liveDataStore.roomParams, answerState: isRight ? 1 : 2 });
    liveDataStore.updateRoomWatchData(roomWatchData)
    // await liveDataStore.fetchRoomWatchData();
  };

  const handleTimeout = async (
    liveDataStore: LiveDataStore
  ) => {
    const roomWatchData = await answer({ ...liveDataStore.roomParams, answerState: 3 });
    liveDataStore.updateRoomWatchData(roomWatchData)

    // liveDataStore.fetchRoomWatchData();
  };

  const initWatcher = (
    liveDataStore: LiveDataStore,
    playerStore: PlayerStore
  ) => {

    // 如果未配置答题或已答题，则不进行监听
    if (liveDataStore.roomData.question.openQuestion !== 1 || liveDataStore.roomWatchData.isAnswer !== 0) {
      return;
    }

    // 完播答题
    if (liveDataStore.roomData.question.hasAnswerQuestion === 0) {
      // TODO: 实现完播答题逻辑
      return;
    }

    // 定时答题
    if (liveDataStore.roomData.question.hasAnswerQuestion === 1) {
      const timeLimit = liveDataStore.roomData.question.showQuestionTimeLimit * 60;

      watch(
        () => playerStore.realProgress,
        (newProgress, oldProgress) => {
          if (!newProgress) return;

          // 答题次数满足设置的要求
          if (liveDataStore.roomData.question.answerNum <= liveDataStore.roomWatchData.answerCount) {
            return
          }

          const newTime = Math.floor(newProgress);
          const oldTime = Math.floor(oldProgress);
          const answerTimeLimit = liveDataStore.roomData.question.answerTimeLimit * 60

          // 处理0分钟答题的情况
          if (liveDataStore.roomData.question.showQuestionTimeLimit == 0) {
            if (liveDataStore.roomWatchData.isAnswer || liveDataStore.roomWatchData.answerState == 3) {
              return
            }
            setIsVisible(true)
            return
          }

          // 处理开始答题时间准点的情况
          if (newTime === timeLimit && newTime !== oldTime) {
            setIsVisible(true);
            return
          }

          // 处理当前时间大于开始答题时间点的情况
          if (newTime > timeLimit) {
            // 当前时间小于结束时间
            if (newTime < timeLimit + answerTimeLimit) {
              // 已经答题
              if (liveDataStore.roomWatchData.isAnswer) {
                return
              }
              // 未答题
              if (isVisible.value) {
                return
              }
              setIsVisible(true)
              return

            } else {
              // 答题时间结束之后
              if (liveDataStore.roomWatchData.isAnswer) {
                return
              }
              // 超时未答题
              liveDataStore.roomWatchData.answerState = 3
            }
          }
        }
      );
    }
  };

  return {
    isVisible,
    isAnswer,
    isComplete,
    isFail,
    setIsVisible,
    setIsAnswer,
    setIsComplete,
    setIsFail,
    initWatcher,
    handleAnswer,
    handleTimeout
  };
});
export type QuestionStore = ReturnType<typeof useQuestionStore>;
